<?php

class InterfaceAction extends Action{

    //获取楼盘城市
    public function get_city(){
        $city = M('loupan')->distinct('city')->getField('city',true);
        echo json_encode($city,true);
    }
    //获取地区
    public function get_district(){
        $city = $_GET['city'];
        $district = M('loupan')->where(array('city'=>$city))->distinct('district')->getField('district',true);
        echo json_encode($district,true);
    }
    public function get_floor_search(){
        $info['score'] = M('loupan')->distinct('score')->getField('score',true);
        $info['name'] = M('loupan')->distinct('name')->getField('name',true);
        echo json_encode($info,true);
        
    }

    //楼盘全部信息
    public function get_loupan_info(){
        $info = M('loupan')->order('createtime desc')->select();
        echo json_encode($info,true);
    }

    //获取楼盘搜索后的信息
    public function get_loupan_search_info(){
        if($_GET['city']){
            if($_GET['city'] != '全部'){
                $map['city'] = array('like','%'.$_GET['city'].'%');
            }
            
        }
        if($_GET['district']){
            if($_GET['district'] != '全部'){
                $map['district'] = array('like','%'.$_GET['district'].'%');
            }
            
        }
        if($_GET['loupan']){
            if($_GET['loupan'] != '全部'){
                $map['name'] = array('like','%'.$_GET['loupan'].'%');
            }
            
        }
        if($_GET['score']){
            if($_GET['score'] != '全部'){
                $map['score'] = array('like','%'.$_GET['score'].'%');
            }
            
        }
        
        $info = M('loupan')->where($map)->order('createtime desc')->select();
        echo json_encode($info,true);
        
    }
    //活动列表搜索
    public function get_active_search(){
        //返回检索条件
        $w_id = $_GET['w_id'];
        $mobile = M('login_redirect')->where('w_id='.$w_id)->getField('account');
        $adminInfo = M('admin')->where('mobile='.$mobile)->find();
        if($adminInfo['role'] == 70){
          $search['principal'] = M('admin')->where('mobile='.$mobile)->getField('truename',true);
        }else{
          $search['principal'] = M('admin')->where('role=70')->getField('truename',true);;
        }
        $search['city'] = M('active')->distinct('addr')->getField('addr',true);
        //今天开始时间戳
        $beginToday = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        //今日结束时间戳
        $endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
        $map['create_time'] = array(array('gt',$beginToday),array('lt',$endToday));
        $search['aname'] = M('active')->where($map)->distinct('aname')->getField('aname',true);
        // $search['aname'] = M('parttime_job')->getField('jpname',true);
        //var_dump($search);
        echo json_encode($search,true);
    }

    //活动列表全部信息
    public function get_active_info(){
        $info = M('active as ac')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.id as actid,ac.addr as city,ac.principal,ac.aname,l.name')
              ->where('l.status=1')
              ->order('ac.create_time desc')
              ->select();
        echo json_encode($info,true);
    }

    //活动列表根据条件搜索
    public function get_active_search_info(){
        if($_GET['addr']){
            if($_GET['addr'] != '全部'){
                $map['ac.addr'] = array('like','%'.$_GET['addr'].'%');
            }
            
        }
        if($_GET['principal']){
            if($_GET['principal'] != '全部'){
                $map['ac.principal'] = array('like','%'.$_GET['principal'].'%');
            }
            
        }
        if($_GET['aname']){
            if($_GET['aname'] != '全部'){
                $map['ac.aname'] = array('like','%'.$_GET['aname'].'%');
            }
            
        }
        $map['l.status'] = 1;
        $map['ac.status'] = 1;
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 24 * 60 * 60 - 1;
        $map['ac.create_time'] = array('between',array($dayBegin,$dayEnd));
        $info = M('active as ac')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.id as actid,ac.addr as city,ac.principal,ac.aname,l.name as loupan')
              ->where($map)
              ->order('ac.create_time desc')
              ->select();
        //var_dump($info);var_dump(M()->getLastSql());
        echo json_encode($info,true);
    }

    //物流运输搜索条件
    public function get_transport_search(){
        $search['driver'] = M('admin')->where(array('role'=>'67'))->getField('truename',true);
        $search['aname'] = M('active')->where('status=1')->distinct('aname')->getField('aname',true);
        $search['loupan'] = M('loupan')->where('status=1')->distinct('name')->getField('name',true);
        $search['status'] = M('active')->distinct('finish')->getField('finish',true);
        echo json_encode($search,true);
    }

    //物流运输全部信息
    public function get_transport_info(){
        $info = M('active as ac')
              ->join("jl_admin as a on a.id=ac.d_id")
              ->join("jl_loupan as l on l.id=ac.name")
              ->field('ac.id as actid,a.truename as driver,ac.aname as program,ac.finish,l.name as loupan,ac.finish')
              ->where('ac.status=1 and l.status=1')
              ->order('ac.create_time')
              ->select();
        echo json_encode($info,true);
    }

    //物流运输按条件搜寻结果
    public function get_transport_search_info(){
        if($_GET['driver']){
            if($_GET['driver'] != '全部'){
                $map['a.truename'] = array('like','%'.$_GET['driver'].'%');
            }
            
        }if($_GET['aname']){
            if($_GET['aname'] != '全部'){
                $map['ac.aname'] = array('like','%'.$_GET['aname'].'%');
            }
            
        }
        if($_GET['loupan']){
            if($_GET['loupan'] != '全部'){
                $map['l.name'] = array('like','%'.$_GET['loupan'].'%');
            }
            
        }
        if($_GET['finish']){
            if($_GET['finish'] != '全部'){
                if($_GET['finish'] == "完成"){
                    $finish = 1;
                }else{
                    $finish = 2;
                }
            }
            
            $map['ac.finish'] = $finish;
        }
        $map['ac.status'] = 1;
        $map['l.status'] = 1;
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 2*24 * 60 * 60 - 1;
        $map['ac.create_time'] = array('between',array($dayBegin,$dayEnd));
        $info = M('active as ac')
              ->join("jl_admin as a on a.id=ac.d_id")
              ->join("jl_loupan as l on l.id=ac.name")
              ->field('ac.id as actid,a.truename as driver,ac.aname as program,ac.finish,l.name as loupan,ac.finish')
              ->where($map)
              ->order('ac.create_time')
              ->select();
        // dump($info);dump(M()->getLastSql());
        echo json_encode($info,true);
    }

    //活动总结搜索条件
    public function get_active_summary_search(){
        $w_id = $_GET['w_id'];
        $mobile = M('login_redirect')->where('w_id='.$w_id)->getField('account');
        $adminInfo = M('admin')->where('mobile='.$mobile)->find();
        if($adminInfo['role'] == 70){
          $search['dudao'] = M('admin')->where('mobile='.$mobile)->getField('truename',true);
        }else{
          $search['dudao'] = M('admin')->where('role=70')->getField('truename',true);;
        }
        $search['aname'] = M('active')->distinct('aname')->where('status=1')->getField('aname',true);
        // $search['dudao'] = M('admin')->where('role=70')->getField('truename',true);
        // $search['loupan'] = M('loupan')->distinct('name')->where('status=1')->getField('name',true);
       
        $start = strtotime(date('Y-m-d', time()));
        $end = $start + 24*3600 -1;
        $map['create_time'] = array('between',array($start,$end));
        $l_id = M('active')->distinct('name')->where($map)->getField('name',true);
        foreach($l_id as $k=>$val){
          $search['loupan'][$k] = M('loupan')->where(array('id'=>$val))->getField('name');
        }
        $search['summary'] = M('active')->distinct('summary')->getField('summary',true);
        echo json_encode($search,true);
    }
    //活动总结总信息
    public function get_active_summary_info(){
        $info = M('active as ac')
                  ->join('jl_loupan as l on l.id=ac.name')
                  ->join('jl_admin as ad on ad.truename=ac.principal')
                  ->join('jl_student as st on st.w_id=ac.w_id')
                  ->field('ac.id,ac.aname as name,st.name as student_name,l.name as loupan,ac.addr as city,ac.principal,ac.dudao,ac.status,ac.create_time,ac.summary')
                  ->order('ac.create_time desc')
                  ->select();
        echo json_encode($info,true);
    }

    //活动总结搜索结果
    public function get_active_summary_search_info(){
        if($_GET['aname']){
            if($_GET['aname'] != '全部'){
                $map['ac.aname'] = array('like','%'.$_GET['aname'].'%');
            }
            
        }
        if($_GET['dudao']){
            if($_GET['dudao'] != '全部'){
                $map['ac.principal'] = array('like','%'.$_GET['dudao'].'%');
            }
            
        }
        if($_GET['loupan']){
            if($_GET['loupan'] != '全部'){
                $map['l.name'] = array('like','%'.$_GET['loupan'].'%');
            }
            
        }
        if($_GET['summary']){
            if($_GET['summary'] != '全部'){
                if($_GET['summary'] == "完成"){
                    $summary = 1;
                }else{
                    $summary = 0;
                }
                $map['ac.summary'] = $summary;
            }
            
            
        }
        // $map['l.status'] = 1;
        $map['ac.status'] = 1;
        // $info = M('active as ac')
        //           ->join('jl_loupan as l on l.id=ac.name')
        //           ->field('ac.id,ac.aname,l.name as loupan,ac.principal as dudao,ac.summary')
        //           ->where($map)
        //           ->order('ac.create_time desc')
        //           ->select();
        
        $info = M('active as ac')
              ->join('jl_loupan as l on l.id=ac.name')
              ->join('jl_admin as ad on ad.truename=ac.principal')
              ->join('jl_student as st on st.w_id=ac.w_id')
              ->field('ac.id,ac.aname,st.name as student_name,l.name as loupan,ac.addr as city,ac.principal as dudao,ac.status,ac.create_time,ac.summary')
              ->order('ac.create_time desc')
              ->where($map)
              ->select();
        echo json_encode($info,true);
    }

    //直播
    public function video_search(){
        // $citys1 = M('video as v')
        //       ->join('jl_active as ac on ac.id=v.a_id')
        //       // ->join('jl_loupan as l on l.id=ac.name')
        //       ->field('ac.addr')
        //       ->group('ac.addr')
        //       ->select();
        $citys1 = M('active')->group('addr')->getField('addr',true);
  // dump(M()->getLastSql());
        // $citys = json_encode($citys1,true);
        // $program1 = M('video as v')
        //       ->join('jl_active as ac on ac.id=v.a_id')
        //       // ->join('jl_loupan as l on l.id=v.l_id')
        //       ->field('ac.aname')
        //       ->group('ac.aname')
        //       ->select();
        $program1 = M('active')->group('aname')->getField('aname',true);
        // $program = json_encode($program1,true);
        // $dudao1 = M('video as v')
        //       ->join('jl_active as ac on ac.id=v.a_id')
        //       // ->join('jl_loupan as l on l.id=ac.name')
        //       ->field('ac.dudao')
        //       ->group('ac.dudao')
        //       ->select();
        $dudao1 = M('active')->group('dudao')->getField('dudao',true);
        // $dudao = json_encode($dudao1,true);
        // $loupan1 = M('video as v')
        //       ->join('jl_loupan as l on l.id=v.l_id')
        //       ->field('l.name as loupan')
        //       ->group('l.name')
        //       ->select();
        // $loupan1 = M('loupan')->where('status=1')->getField('name',true);
        $start = strtotime(date('Y-m-d', time()));
        $end = $start + 24*3600 -1;
        $map['create_time'] = array('between',array($start,$end));
        $l_id = M('active')->distinct('name')->where($map)->getField('name',true);
        foreach($l_id as $k=>$val){
          $loupan1[$k] = M('loupan')->where(array('id'=>$val))->getField('name');
        }
        $data['citys'] = $citys1;
        $data['program'] = $program1;
        $data['dudao'] = $dudao1;
        $data['loupan'] = $loupan1;
        // dump($data);
        // $loupan = json_encode($loupan1,true);
        // dump($citys1);dump($program1);dump($dudao1);dump($loupan1);
        // if($citys1 && $program1 && $dudao1 && $loupan1){
            echo json_encode($data,true);
            // $this->ajaxReturn(array('msg'=>0,'data'=>$data));

        // }else{
        //     $this->ajaxReturn(array('msg'=>1,'data'=>'error'));
        // }
    }
    //全部信息
    public function video_info(){
        $info = M('video as v')
              ->join('jl_active as ac on ac.id=v.a_id')
              ->join('jl_loupan as l on l.id=ac.name')
              ->field('ac.addr,ac.aname,l.name,ac.dudao,v.id,v.url')
              ->select();
        echo json_encode($info,true);
        // if($info){

        //     $this->ajaxReturn(array('msg'=>0,'data'=>array('info'=>$info)));
        // }else{
        //     $this->ajaxReturn(array('msg'=>1,'data'=>'error'));
        // }
    }

    //搜索信息
    public function video_search_info(){
        if($_GET['addr']){
            if($_GET['addr'] != '全部'){
                $map['ac.addr'] = array('like','%'.$_GET['addr'].'%');
            }
            
        }
        if($_GET['aname']){
            if($_GET['aname'] != '全部'){
                $map['ac.aname'] = $_GET['aname'];
            }
            
        }
        if($_GET['dudao']){
            if($_GET['dudao'] != '全部'){
                $map['ac.dudao'] = $_GET['dudao'];
            }
            
        }
        if($_GET['name']){
            if($_GET['name'] != '全部'){
                $map['l.name'] = array('like','%'.$_GET['name'].'%');
            }
            
        }
        // 当天的零点
        $dayBegin = strtotime(date('Y-m-d', time())); 
        // 当天的24
        $dayEnd = $dayBegin + 24 * 60 * 60 - 1;
        $map['v.a_time'] = array('between',array($dayBegin,$dayEnd));
        $info = M('active as ac')
              ->join('jl_video as v on ac.aname=v.a_id')
              ->join('jl_loupan as l on l.id=v.l_id')
              ->field('ac.addr,ac.aname,l.name as loupan,ac.dudao,v.id,v.url,v.a_time')
              ->where($map)
              ->group('l.name')
              ->select();
        // var_dump($info);var_dump(M()->getLastSql());
       echo json_encode($info,true);
    }

    //根据日期选择活动
    public function get_active_by_time(){
      // $_POST['time'] = 1502985600;
      $map['create_time'] = array('gt',strtotime($_POST['time']));
      $info1 = M('active')->where($map)->group('aname')->select();
      foreach($info1 as $k=>$val){
        $info[$k]['id'] = $val['id'];
        $info[$k]['program'] = $val['aname'];
      }
      // dump($info);
      if($info){
        echo json_encode(array('msg'=>0,'val'=>$info),true);
      }else{
        echo json_encode(array('msg'=>1,'val'=>'error'),true);
      }
    }

    //根据日期,活动名返回楼盘信息
    public function get_loupan_by_time_and_active(){
      // $_POST['time'] = 1502985600;
      // $map['ac.aname'] = 'test0819';
      $map['ac.create_time'] = array('gt',strtotime($_POST['time']));
      $map['ac.aname'] = M('active')->where(array('aname'=>$_POST['program']))->getField('aname');
      $info = M('active as ac')
            ->join('jl_loupan as l on ac.name=l.id')
            ->where($map)
            ->field('l.id as l_id,l.name as loupan')
            ->select();
      // dump($info);
      if($info){
        echo json_encode(array('msg'=>0,'val'=>$info),true);
      }else{
        echo json_encode(array('msg'=>1,'val'=>'error'),true);
      }
    }
    //新增直播
    public function add_video_c(){
      if(IS_POST){
          $data['a_id'] = $_POST['program'];
          $data['l_id'] = $_POST['loupan'];
          $data['a_time'] = strtotime($_POST['time']);
          $url = M('camera')->where('id='.$_POST['url'])->getField('sn');
          $data['url'] = "http://shuidi.huajiao.com/mobile/offline.html?sn=".$url;
          $exist = M('video')->where($data)->find();
          if($exist){
              echo json_encode(array('msg'=>1,'val'=>'此数据已存在'),true);
          }else{
              $data['createtime'] = time();
              $add = M('video')->add($data);
              if($add){
                  // $this->success('提交成功',U('video_list'));
                echo json_encode(array('msg'=>0),true);
              }else{
                  // $this->error('请重新尝试');
                  echo json_encode(array('msg'=>2,'val'=>'请重新尝试'),true);
              }
          }
      }
    }

    //兼职筛选
    public function get_parttime_job_search_info(){
        if($_GET['addr']){
            if($_GET['addr'] != '全部'){
                 $map['job_name'] = array('like','%'.$_GET['addr'].'%');
            }
            
        }
        if($_GET['principal'] == '正序'){
            $order = "start_day";
        }else{
            $order = "start_day desc";
        }
        if($_GET['aname'] == '正序'){
            $order1 = "salary";
        }else{
            $order1 = "salary desc";
        }
        $map['status'] = 1;
        $map['end_day'] = array('gt',time()-18*3600);
        $info = M('parttime_job')->where($map)->field('id as j_id,job_name,start_day,salary')->order($order,$order)->select();
        foreach($info as $k=>$val){
            $info[$k]['start_day'] = date('Y-m-d',$val['start_day']);
        }
        //var_dump(M()->getLastSql());
        echo json_encode($info,true);
    }


    //新增活动时,根据所选的日期和城市获取合适的促销生
    public function get_cuxiao(){
        $map['s.addr'] = $_POST['city'];
        $map['p.start_day'] = array('elt',strtotime($_POST['active_time']));
        $map['p.end_day'] = array('egt',strtotime($_POST['active_time']));
        $map['sa.status'] = 1;
        //$map['sa.type'] = 0;
        $info = M('student as s')
              ->join('LEFT JOIN jl_sales_join as sa on sa.w_id=s.w_id')
              ->join('LEFT JOIN jl_parttime_job as p on p.id=sa.j_id')
              ->where($map)
              ->distinct('s.name')
              ->field('s.w_id as id,s.name as text')
              ->select();

          // $sql = M()->getLastSql();
        if($info){
            echo json_encode(array("msg"=>0,"val"=>$info,'sql'=>$sql));
        }else{
            echo json_encode(array("msg"=>1,"val"=>"error",'sql'=>$sql));
        }
    }
}
